<template>
  <div class="login-container">
    <div class="login-card">
      <div class="logo-container">
        <div class="logo">logo</div>
        <p class="welcome-text">欢迎加入 TodoList，开启高效生活</p>
      </div>
      
      <el-form :model="loginForm" ref="loginFormRef" :rules="rules">
        <div class="form-item">
          <div class="form-label">邮箱地址</div>
          <el-input 
            v-model="loginForm.email" 
            placeholder="请输入您的邮箱地址"
            :prefix-icon="Message">
          </el-input>
        </div>
        
        <div class="form-item">
          <div class="form-label">密码</div>
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请再次输入密码"
            :prefix-icon="Lock"
            show-password>
          </el-input>
        </div>
        
        <div class="form-item remember-forgot">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          <span class="forgot-password">忘记密码?</span>
        </div>
        
        <el-button 
          type="primary" 
          class="login-button" 
          :loading="loading" 
          @click="handleLogin">
          登录
        </el-button>
      </el-form>
      
      <div class="divider">
        <span>其他登录方式</span>
      </div>
      
      <div class="social-login">
        <div class="social-icon wechat"><img src="../assets/wechat.png" alt="微信" /></div>
        <div class="social-icon qq"><img src="../assets/qq.png" alt="QQ" /></div>
        <div class="social-icon weibo"><img src="../assets/weibo.png" alt="微博" /></div>
      </div>
      
      <div class="register-link">
        <span>没有账号? <router-link to="/register" class="link">立即注册</router-link></span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Lock, Message } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const rememberMe = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  email: '',
  password: ''
})

const rules = {
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为6个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const response = await fetch('/api/auth/login/', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        email: loginForm.email,
        password: loginForm.password
      })
    })
    
    if (!response.ok) {
      throw new Error('登录失败，请检查您的账号和密码')
    }
    
    const data = await response.json()
    
    // 存储令牌
    localStorage.setItem('access_token', data.access)
    localStorage.setItem('refresh_token', data.refresh)
    
    // 如果有用户信息，则也存储
    if (data.user) {
      localStorage.setItem('user', JSON.stringify(data.user))
    }
    
    ElMessage.success('登录成功')
    
    // 跳转到首页
    router.push('/')
  } catch (error) {
    ElMessage.error(error.message || '登录失败，请稍后再试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #bcd7ff 0%, #a3c1ff 100%);
  position: relative;
  overflow: hidden;
}

.login-card {
  width: 400px;
  background: #fff;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.logo-container {
  text-align: center;
  margin-bottom: 24px;
}

.logo {
  font-size: 32px;
  font-weight: bold;
  color: #6366f1;
  margin-bottom: 8px;
  font-style: italic;
}

.welcome-text {
  color: #666;
  font-size: 14px;
  margin-top: 8px;
}

.form-item {
  margin-bottom: 20px;
}

.form-label {
  margin-bottom: 8px;
  font-size: 14px;
  color: #333;
}

.login-button {
  width: 100%;
  height: 44px;
  border-radius: 6px;
  font-size: 16px;
  margin-top: 8px;
  background-color: #6366f1;
  border-color: #6366f1;
}

.login-button:hover {
  background-color: #4f46e5;
  border-color: #4f46e5;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.forgot-password {
  color: #6366f1;
  cursor: pointer;
  font-size: 14px;
}

.forgot-password:hover {
  text-decoration: underline;
}

.divider {
  display: flex;
  align-items: center;
  margin: 24px 0;
  color: #999;
  font-size: 14px;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #eee;
}

.divider span {
  padding: 0 16px;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-bottom: 24px;
}

.social-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.2s;
  background: #f5f5f5;
}

.social-icon:hover {
  transform: translateY(-3px);
}

.social-icon img {
  width: 24px;
  height: 24px;
}

.register-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.link {
  color: #6366f1;
  text-decoration: none;
}

.link:hover {
  text-decoration: underline;
}
</style> 